package com.myGroup.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.myGroup.dto.ActivityDTO;
import com.myGroup.dto.UserDTO;
import com.myGroup.pojo.Activity;
import com.myGroup.pojo.R;
import com.myGroup.pojo.User;
import com.myGroup.service.ActivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.ResultMap;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;

@RestController
@Slf4j
@Api(tags = "活动管理接口")
@RequestMapping("/admin/act")
public class ActivityController {
    @Autowired
    private ActivityService activityService;


    /**
     * 新增活动
     *
     * @param activityDTO
     * @return
     */
    @ApiOperation("新增活动")
    @PostMapping()
    public R<String> save(@RequestBody ActivityDTO activityDTO) {
        Activity activity = new Activity();
        BeanUtils.copyProperties(activityDTO, activity);
        activityService.save(activity); // 操作service
        return R.success("活动添加成功");
    }

    /**
     * 根据id修改活动
     *
     * @param activity
     * @return
     */
    @PutMapping()
    @ApiOperation("根据id修改活动")
    public R<String> update(@RequestBody Activity activity) {
        activityService.updateById(activity);
        return R.success("活动修改成功");
    }

    /**
     * 根据id查询活动
     *
     * @param id
     * @return
     */
    @GetMapping("/dtl/{id}")
    @ApiOperation("根据id查询活动")
    public R<Activity> selectById(@PathVariable Integer id) {
        return R.success(activityService.getById(id));
    }

    /**
     * 活动分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @param place
     * @param org
     * @param begin
     * @param end
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("活动分页查询")
    public R<Page> page(@RequestParam(defaultValue = "1") Integer page,
                        @RequestParam(defaultValue = "10") Integer pageSize,
                        @ApiParam("活动名称") String name,
                        @ApiParam("活动地点") String place,
                        @ApiParam("活动举办方") String org,
                        @ApiParam(value = "查询开始时间，格式：yyyy-MM-dd", defaultValue = "2023-10-29")
                        @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                        @ApiParam(value = "查询结束时间，格式：yyyy-MM-dd", defaultValue = "2023-10-31")
                        @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {

        //构造分页构造器
        Page pageInfo = new Page(page, pageSize);
        //条件限定
        LambdaQueryWrapper<Activity> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(StringUtils.isNotEmpty(name), Activity::getName, name)
                .like(StringUtils.isNotEmpty(place), Activity::getPlace, place)
                .like(StringUtils.isNotEmpty(org), Activity::getOrg, org)
                .ge((begin != null), Activity::getBeginTime, begin)
                .le((end != null), Activity::getBeginTime, end);

        //排序
        queryWrapper.orderByAsc(Activity::getId);
        //执行
        activityService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }
}
